﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace CSP.Package.Controls
{
    public partial class Initialization : UserControl, IInstallationControl
    {
        public Initialization()
        {
            InitializeComponent();
        }

        #region IInstallationControl Members

        protected Installer _form;
        public Installer GetForm()
        {
            return _form;
        }

        public void SetForm(Installer installer)
        {
            _form = installer;
        }

        public bool ValidateInputs()
        {
            if (txbUrl.Text.Length == 0)
            {
                MessageBox.Show(CSP.Package.labels.CspInstallError_UrlNoEmpty, CSP.Package.labels.CspInstallError_Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbUrl.Focus();
                txbUrl.Select();
                return false;
            }
            if (txbIdentity.Text.Length == 0)
            {
                MessageBox.Show(CSP.Package.labels.CspInstallError_UrlNoEmpty, CSP.Package.labels.CspInstallError_Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbIdentity.Focus();
                txbIdentity.Select();
                return false;
            }
            return true;

        }

        public bool Next()
        {
            
            GetForm().AddToLog("----------------------------------------------");
            GetForm().AddToLog(CSP.Package.labels.CspInstallLog_InstallationInit);
            if (ValidateInputs())
            {
                try
                {
                    CSP.Package.RegistryAccess.CreateStringProperty(Registry.LocalMachine, Constants.CspRegistryKey, "CentralAdminUrl", txbUrl.Text);
                    CSP.Package.RegistryAccess.CreateStringProperty(Registry.LocalMachine, Constants.CspRegistryKey, "CentralAdminIdentity", txbIdentity.Text);
                }
                catch (Exception ex)
                {
                    this.GetForm().ShowException(ex);
                    throw ex;
                }
                return true;
            }
            return false;
        }

        public void Previous()
        {
        }

        public void Skip()
        {
        }

        #endregion

        private void Initialization_Load(object sender, EventArgs e)
        {
            TranslateLiterals();
        }

        private void TranslateLiterals()
        {
            richTextBox1.Text = CSP.Package.labels.Initialization_Intro;

            groupBox1.Text = CSP.Package.labels.CentralAdministrationInformation_GroupBox;
          
            label5.Text = CSP.Package.labels.Url_Label;
            label2.Text = CSP.Package.labels.AppPoolIdentity_Label;         
        }

        
    }
}
